﻿// -----------------------------------------------------------------------
// <copyright file="BarManagerAdapter.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

using System.Collections.Specialized;
using DevExpress.Xpf.Bars;
using DevExpress.Xpf.Ribbon;
using Microsoft.Practices.Prism.Regions;

namespace Abc.Desktop.Adapters
{
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class BarManagerAdapter : RegionAdapterBase<BarManager>
    {

        public BarManagerAdapter(IRegionBehaviorFactory regionBehaviorFactory)
            : base(regionBehaviorFactory)
        {
        }

        protected override void Adapt(IRegion region, BarManager regionTarget)
        {
            region.Views.CollectionChanged += (s, e) =>
            {
                switch (e.Action)
                {
                    case NotifyCollectionChangedAction.Add:
                        foreach (var elt in e.NewItems)
                        {
                            if (elt is BarItem)
                            {
                                regionTarget.Items.Add(elt as BarItem);
                            }
                            if (elt is BarManager)
                            {var bm = elt as BarManager;
                                IRibbonControl ribbonControl = (RibbonControl)bm.Child;
                                IRibbonControl targetRibbonControl = BarManagerHelper.GetRibbonControl(regionTarget);
                                targetRibbonControl.Merge(ribbonControl, null);
                            }
                        }
                        break;
                }
            };
        }

        protected override IRegion CreateRegion()
        {
            return new AllActiveRegion();
        }
    }
}
